package com.xjnt.base.support.admin.controller;

import com.xjnt.base.support.admin.entity.Permission;
import com.xjnt.base.support.admin.service.DictionaryService;
import com.xjnt.base.support.admin.service.FunctionService;
import com.xjnt.base.support.admin.service.MenuService;
import com.xjnt.base.support.admin.service.PermissionService;
import com.xjnt.base.support.admin.service.PlatformService;
import com.xjnt.frameworks.annotation.AutoInject;
import com.xjnt.frameworks.annotation.Router;
import com.xjnt.frameworks.core.CRUDController;
import com.xjnt.frameworks.exception.BusinessException;
import com.xjnt.frameworks.web.message.RetMsg;

@Router(name = "/system/admin/permission", view = "/admin/permission")
public class PermissionController extends CRUDController<Permission, PermissionService> {

	@AutoInject
	private PermissionService permissionService;
	@AutoInject
	private PlatformService platformService;
	@AutoInject
	private MenuService menuService;
	@AutoInject
	private FunctionService functionService;
	@AutoInject
	private DictionaryService dictionaryService;

	@Override
	protected String getBizName() {
		return "权限";
	}
	
	@Override
	protected Class<Permission> getClazz() {
		return Permission.class;
	}

	@Override
	protected PermissionService getService() {
		return permissionService;
	}

	@Override
	protected void addBefore() {
		Integer permType = getParaToInt("type");
		switch (permType) {
			case 0:
				setAttr("relation", platformService.notAllocPlatform());
				break;
			case 1:
				setAttr("relation", menuService.notAllocModule());
				break;
			case 2:
				setAttr("relation", menuService.notAllocMenu());
				break;
			case 3:
				setAttr("relation", functionService.notAllocFunction());
				break;
			default:
				break;
		}
	}
	
	@Override
	public void editBefore(Permission model) {
		Integer permType = model.getInt(Permission.TYPE);
		String permId = model.getStr(Permission.PK_ID);
		switch (permType) {
			case 0:
				setAttr("relation", platformService.findByPermId(permId));
				break;
			case 1:
				setAttr("relation", menuService.findByPermId(permId));
				break;
			case 2:
				setAttr("relation", menuService.findByPermId(permId));
				break;
			case 3:
				setAttr("relation", functionService.findByPermId(permId));
				break;
			default:
				break;
		}
	}

	@Override
	public void doSave() {
		RetMsg retMsg = new RetMsg();
		try {
			Permission model = getModel(getClazz());
			String relationId = getPara("relationId");
			boolean flag = getService().save(model, relationId);
			if (flag) {
				retMsg.pushOk("保存成功");
			} else {
				retMsg.pushError("保存失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("保存" + getBizName(), "发生异常：", e);
		}
	}

	@Override
	public void doUpdate() {
		RetMsg retMsg = new RetMsg();
		try {
			Permission model = getModel(getClazz());
			String relationId = getPara("relationId");
			boolean flag = getService().update(model, relationId);
			if (flag) {
				retMsg.pushOk("更新成功");
			} else {
				retMsg.pushError("更新失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("更新" + getBizName(), "发生异常：", e);
		}
	}
}
